<?php



namespace app\bbxw\controller\api;



use app\bbxw\service\WechatService;
use app\bbxw\service\UserAdminService;
use think\admin\Controller;
use app\bbxw\model\WxUser as WxUserMod;
use think\admin\extend\CodeExtend;
use think\admin\model\SystemUser;

/**
 * 陪玩接口
 * Class Login
 * @package app\data\controller\api
 */
class Power extends Controller
{
    //首页数据
    public function index()
    {
        $power_source_id = $this->request->get('id');
        $power_source = $this->app->db->name("power_source")->where(['id'=>$power_source_id])->find();
        $grade_image = $this->app->db->name("power_grade")->where(['id'=>$power_source['grade']])->value("image");
        $total = $this->app->db->name("play_accompany")->where(['power_source'=>$power_source['name'],'status'=>1])->sum("business_amount");
        $today = $this->app->db->name("play_accompany")
            ->where(['power_source'=>$power_source['name'],'status'=>1])
            ->whereDay("date",'yesterday')
            ->sum("business_amount");
        $week = $this->app->db->name("play_accompany")
            ->where(['power_source'=>$power_source['name'],'status'=>1])
            ->whereWeek("date")
            ->sum("business_amount");
        $day = date('d');
        if($day>15){
            $star_date = date('Y-m-15');
        }else{
            $star_date = date('Y-m-15 ',strtotime("-1 month"));

        }
        $end_date = date('Y-m-d');
        $month = $this->app->db->name("play_accompany")
            ->where(['power_source'=>$power_source['name'],'status'=>1])
            ->whereBetweenTime("date",$star_date,$end_date)
            ->sum("business_amount");

        $ranking = $this->app->db->name("play_accompany")
            ->field("sum(business_amount) as total,power_source")
            ->whereBetweenTime("date",$star_date,$end_date)
            ->group("power_source")->order('total desc')->limit(0,3)->select()->toArray();
        foreach ($ranking as $k=>$v){
            $ranking[$k]['harder_img'] = $this->app->db->name("power_source")->where(['name'=>$v['power_source']])->value("harder_img");
            $ranking[$k]['icon'] = "medal";
            switch ($k){
                case 0:
                    $ranking[$k]['iconColor'] = "#FFD700";
                    break;
                case 1:
                    $ranking[$k]['iconColor'] = "#C0C0C0";
                    break;
                case 2:
                    $ranking[$k]['iconColor'] = "#CD7F32";
                    break;
            }
        }
        $this->success('获取成功',['power_source'=>$power_source,'grade_image'=>$grade_image,'total'=>$total,'today'=>$today,'week'=>$week,'month'=>$month,'ranking'=>$ranking]);
    }

    public function power_source()
    {
        $id = $this->request->get('id');
        $power_source = $this->app->db->name("power_source")->where(['id'=>$id])->find();
        $this->success('获取成功',$power_source);
    }

    public function orderlist()
    {
        $power_source_id = $this->request->get('id');
        $power_source_name = $this->app->db->name("power_source")->where(['id'=>$power_source_id])->value('name');
        $orderDay = $this->app->db->name("play_accompany")
            ->where(['power_source_id'=>$power_source_name])
            ->whereDay("date")->order('id desc')->select()->toArray();
        $orderWeek = $this->app->db->name("play_accompany")
            ->where(['power_source_id'=>$power_source_name])
            ->whereWeek("date")
            ->order('id desc')
            ->select()->toArray();
        $orderMonth = $this->app->db->name("play_accompany")
            ->where(['power_source_id'=>$power_source_name])
            ->whereMonth("date")
            ->order('id desc')->select()->toArray();
        foreach ($orderDay as $k=>$v){
            $orderDay[$k]['date'] = date("Y-m-d",strtotime($v['date']));
        }
        foreach ($orderWeek as $k=>$v){
            $orderWeek[$k]['date'] = date("Y-m-d",strtotime($v['date']));
        }
        foreach ($orderMonth as $k=>$v){
            $orderMonth[$k]['date'] = date("Y-m-d",strtotime($v['date']));
        }
        $this->success('获取成功',['orderDay'=>$orderDay,'orderWeek'=>$orderWeek,'orderMonth'=>$orderMonth]);
    }

    public function hyqy()
    {
        $power_source_id = $this->request->get('id');
        $power_source = $this->app->db->name("power_source")->where(['id'=>$power_source_id])->find();
        $grade = $this->app->db->name("power_grade")->where(['id'=>$power_source['grade']])->find();
        // 将多行文本按换行符分割成数组
        $remarkLines = explode("\n", $grade['remark']);
        // 去除每一行的前后空格
        $remarkLines = array_map('trim', $remarkLines);
        // 去除空行
        $remarkLines = array_filter($remarkLines, function($line) {
            return !empty($line);
        });
        // 将处理后的数组赋值给 $grade['remark']
        $grade['remark_arr'] = array_values($remarkLines); // 重新索引数组
       // dump($grade['remark']);
        $next_grade = $this->app->db->name("power_grade")->where(['id'=>$power_source['grade']+1])->find();
        $percent = 100;
        $total_amount = $this->app->db->name("play_accompany")
            ->where(['power_source'=>$power_source['name']])->whereTime('date','>',"2024-09-16 00:00:00")->sum("business_amount");
        if($next_grade){
            $percent = round(($total_amount/$next_grade['amount'])*100,2);
        }
        $this->success('获取成功',[
            'power_source'=>$power_source,
            'grade'=>$grade,
            'total_amount'=>$total_amount,
            'next_grade'=>$next_grade,
            'percent'=>$percent,
            'next_amount'=>bcsub((string)$next_grade['amount'],(string)$total_amount,2)
        ]);
    }


    public function naming()
    {
        $customer_id = $this->request->get('id');
        $naming = $this->app->db->name("naming")->where(['customer_id'=>$customer_id])->select()->toArray();
        $list = array_column($naming,"image");
        $birthday = $this->app->db->name("customer")->where(['id'=>$customer_id])->value("birthday");
        $this->success('获取成功',["list"=>$list,"naming"=>$naming,'birthday'=>$birthday]);
    }

    public function getGoods()
    {
        $goods = $this->app->db->name("goods")->where(['is_deleted'=>0])->order('sort ASC')->select()->toArray();
        $this->success('获取成功',$goods);
    }
    public function exchangeOrder()
    {
        $customer_id = $this->request->get('id');
        $list = $this->app->db->name("exchange_order")->where(['customer_id'=>$customer_id])->order('id desc')->select()->toArray();
        $this->success('获取成功',$list);
    }

    public function exchange()
    {
        $customer_id = $this->request->get('id');
        $goods_id = $this->request->get('goods_id');
        $goods = $this->app->db->name("goods")->where(['id'=>$goods_id])->find();
        if($goods['stock']<=0){
            $this->error('库存不足');
        }
        $customer = $this->app->db->name("customer")->where(['id'=>$customer_id])->find();
        if(($customer['points']-$customer['use_points'])<$goods['price']){
            $this->error('积分不足');
        }
        $this->app->db->name("customer")->where(['id'=>$customer_id])->update(['use_points'=>$customer['use_points']+$goods['price']]);
        $this->app->db->name("goods")->where(['id'=>$goods_id])->update(['stock'=>$goods['stock']-1]);
        $this->app->db->name("exchange_order")->insert([
            'customer_id'=>$customer_id,
            'goods_id'=>$goods_id,
            'order_no'=>CodeExtend::uniqidDate(18, 'NO'),
            'amount'=>$goods['price'],
            'customer_name'=>$customer['name'],
            'goods_info' => json_encode($goods),
            'goods_name' => $goods['title']
        ]);
        $this->success('兑换成功');
    }

    public function savebirthday()
    {
        $customer_id = $this->request->get('id');
        $birthday = $this->request->get('birthday');
        $this->app->db->name("power_source")->where(['id'=>$customer_id])->update(['birthday'=>$birthday]);
        $this->success('保存成功');
    }

    public function saveHeadimg()
    {
        $customer_id = $this->request->get('id');
        $harder_img = $this->request->get('harder_img');
        $this->app->db->name("power_source")->where(['id'=>$customer_id])->update(['harder_img'=>$harder_img]);
        $this->success('保存成功');
    }

    public function ranking()
    {

        $week = $this->app->db->name("play_accompany")
            ->field("sum(business_amount) as total,power_source")
            ->whereWeek("date")->group("power_source")->order('total desc')->limit(0,10)->select()->toArray();
        $day = date('d');
        if($day>15){
            $star_date = date('Y-m-15');
        }else{
            $star_date = date('Y-m-15 ',strtotime("-1 month"));

        }
        $end_date = date('Y-m-d');
        $month = $this->app->db->name("play_accompany")
            ->field("sum(business_amount) as total,power_source")->whereBetweenTime("date",$star_date,$end_date)
            ->group("power_source")->order('total desc')->limit(0,10)->select()->toArray();

        foreach ($week as $k=>$v){
            $week[$k]['harder_img'] = $this->app->db->name("power_source")->where(['name'=>$v['power_source']])->value("harder_img");
        }
        foreach ($month as $k=>$v){
            $month[$k]['harder_img'] = $this->app->db->name("power_source")->where(['name'=>$v['power_source']])->value("harder_img");
        }
        $this->success('获取成功',[
            'week'=>$week,
            'month'=>$month
        ]);
    }

    public function rankingIndex()
    {

    }

    //消费明细 按日期展示
    public function consumption()
    {
        $customer_id = $this->request->get('id');
        $power_source_name = $this->app->db->name("power_source")->where(['id'=>$customer_id])->value('name');
        $dates = [];
        for ($i = 0; $i < 7; $i++) {
            $dates[] = date('Y-m-d', strtotime("-$i day"));
        }
        $list = [];
        $weeks = ["周日", "周一", "周二", "周三", "周四", "周五", "周六"];
        $weeks_en = ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"];
        foreach ($dates as $k=>$v){
            $list[$k]['business_amount'] = $this->app->db->name("play_accompany")->where(['power_source'=>$power_source_name])
                ->whereDay("date",$v)->order('id desc')->sum('business_amount');
            $list[$k]['date'] = $v;
            $week = date('w',strtotime($v));
            $list[$k]['week'] = $weeks[$week];
            $list[$k]['week_en'] = $weeks_en[$week];
        }
        $this->success('获取成功',['list'=>$list]);
    }

    public function dayList()
    {
        $customer_id = $this->request->get('id');
        $power_source_name = $this->app->db->name("power_source")->where(['id'=>$customer_id])->value('name');
        $date = $this->request->get('date');
        $list = $this->app->db->name("play_accompany")->where(['power_source'=>$power_source_name,'status'=>1])
            ->whereDay("date",$date)->whereNotNull("business_amount")->order('id desc')->select()->toArray();
        $total = $this->app->db->name("play_accompany")->where(['power_source'=>$power_source_name,'status'=>1])
            ->whereDay("date",$date)->order('id desc')->sum("business_amount");
        $this->success('获取成功',['list'=>$list,'total'=>$total]);

    }


    //流水记录统计
    public function orderTotal()
    {
        $power_source_id = $this->request->get('id');
        $power_source_name = $this->app->db->name("power_source")->where(['id'=>$power_source_id])->value('name');
        $total = $this->app->db->name("play_accompany")
            ->where(['power_source'=>$power_source_name,'status'=>1])->sum("business_amount");
        $total_num = $this->app->db->name("play_accompany")
            ->where(['power_source'=>$power_source_name,'status'=>1])->count();
        $day = date('d');
        if($day>15){
            $star_date = date('Y-m-15');
        }else{
            $star_date = date('Y-m-15 ',strtotime("-1 month"));

        }
        $end_date = date('Y-m-d');
        $month = $this->app->db->name("play_accompany")
            ->where(['power_source'=>$power_source_name,'status'=>1])
            ->whereBetweenTime("date",$star_date,$end_date)
            ->sum("business_amount");
        $month_num = $this->app->db->name("play_accompany")
            ->where(['power_source'=>$power_source_name,'status'=>1])
            ->whereBetweenTime("date",$star_date,$end_date)
            ->count();
        $this->success('获取成功',['total'=>$total,'month'=>$month,'total_num'=>$total_num,'month_num'=>$month_num]);

    }

    public function mindinfo()
    {
        $power_source_id = $this->request->get('id');
        $power_source = $this->app->db->name("power_source")->where(['id'=>$power_source_id])->find();
        $grade_info = $this->app->db->name("power_grade")->where(['id'=>$power_source['grade']])->find();
        $this->success('获取成功',['power_source'=>$power_source,'grade_info'=>$grade_info]);
    }

    public function chagePassword()
    {
        $power_source_id = $this->request->post('id');
        $password = $this->request->post('password');
        $this->app->db->name("power_source")->where(['id'=>$power_source_id])->update(['password'=>md5($password)]);
        $this->success('修改成功');
    }


}
